import { Model, DataTypes } from 'sequelize'
import { sequelize } from "../config/index.js"
import Post from './Post.js'
import Tag from './Tag.js'

// 定义一个 PostTag 类 继承 Model
class PostTag extends Model {}

// 通过 init 方法定义表的字段等信息
PostTag.init(
  {
    postID: {
      type: DataTypes.INTEGER,
      references: {
        model: Post,
        key: 'id',
      },
    },
    tagID: {
      type: DataTypes.INTEGER,
      references: {
        model: Tag,
        key: 'id',
      },
    },
  },
  { sequelize, modelName: 'PostTag', timestamps: false }
)

//设置 Tag 表与 Post 表之间的关系 (多对多)
Post.belongsToMany(Tag, { through: PostTag, foreignKey: 'postID' });
Tag.belongsToMany(Post, { through: PostTag, foreignKey: 'tagID' });

export default PostTag;